﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：Pool
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 18:01:39
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.Collections.Generic;

namespace TouchFish.ObjectPool
{
    public abstract class Pool<T> : IPool<T>, ICountObserveAble
    {
        #region ICountObserverable

        /// <summary>
        /// Gets the current count.
        /// </summary>
        /// <value>The current count.</value>
        public int CurCount
        {
            get { return mCacheStack.Count; }
        }

        #endregion ICountObserverable

        protected IObjectFactory<T> mFactory;

        protected readonly Stack<T> mCacheStack = new Stack<T>();

        /// <summary>
        /// default is 5
        /// </summary>
        protected int mMaxCount = 12;

        public virtual T Allocate()
        {
            return mCacheStack.Count == 0
                ? mFactory.Create()
                : mCacheStack.Pop();
        }

        public abstract bool Recycle(T obj);
    }
}